package com.xinzhidi.web.dataManagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xinzhidi.common.annotation.Log;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.enums.BusinessType;
import com.xinzhidi.common.utils.bean.BeanUtils;
import com.xinzhidi.common.utils.poi.ExcelUtil;
import com.xinzhidi.web.build.domain.BuildUnit;
import com.xinzhidi.web.build.domain.CBuild;
import com.xinzhidi.web.build.service.IBuildUnitService;
import com.xinzhidi.web.build.service.ICBuildService;
import com.xinzhidi.web.dataManagement.domain.bo.FiledControl;
import com.xinzhidi.web.dataManagement.domain.po.DataManagementPo;
import com.xinzhidi.web.dataManagement.domain.vo.DataManagementBuildVo;
import com.xinzhidi.web.dataManagement.domain.vo.DataManagementUnitVo;
import com.xinzhidi.web.dataManagement.domain.vo.DataManagementVo;
import com.xinzhidi.web.dataManagement.service.DataManagementService;
import com.xinzhidi.web.netty.utils.SecurityAdminUtils;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据管理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/dataManagement")
@RequiredArgsConstructor
public class DataManagementController extends BaseController {

    private final DataManagementService managementService;

    private final IBuildUnitService buildUnitService;

    private final ICBuildService buildService;


    @ApiOperation("楼栋-详细数据查询")
    @PostMapping("/listBuild")
    public TableDataInfo<DataManagementVo> listBuild(@Validated @RequestBody DataManagementPo po) {
        List<DataManagementVo> list = Lists.newArrayList();

        List<Long> userPlotIds = SecurityAdminUtils.getUserPlotIds();
        po.setUserPlotIds(userPlotIds);
        //通过小区id查询楼栋id
        LambdaQueryWrapper<CBuild> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CBuild::getPlotId, userPlotIds);
        List<CBuild> buildUnits = buildService.list(queryWrapper);
        if(CollectionUtils.isEmpty(buildUnits)){
            return TableDataInfo.getDataTable(list);
        }
        List<Long> buildIds = buildUnits.stream().map(CBuild::getId).collect(Collectors.toList());
        po.setBuildIds(buildIds);
        startPagePost(po);
        return managementService.listBuild(po);
    }


    @ApiOperation("导出楼栋-详细数据")
    @Log(title = "导出楼栋-详细数据", businessType = BusinessType.EXPORT)
    @PostMapping("/listBuildExport")
    public void listBuildExport(HttpServletResponse response, @Validated DataManagementPo po) {
        po.setPageSize(999999999);
        TableDataInfo<DataManagementVo> list = managementService.listBuild(po);
        ExcelUtil<DataManagementBuildVo> util = new ExcelUtil<DataManagementBuildVo>(DataManagementBuildVo.class);
        util.exportExcel(response, list.getRows().stream().map(item -> {
            DataManagementBuildVo vo = new DataManagementBuildVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList()), "楼栋-详细数据");
    }

    @ApiOperation("单元-详细数据查询")
    @PostMapping("/listBuildUnit")
    public TableDataInfo<DataManagementVo> listBuildUnit(@Validated @RequestBody DataManagementPo po) {
        List<DataManagementVo> list = Lists.newArrayList();
        List<Long> userPlotIds = SecurityAdminUtils.getUserPlotIds();
        po.setUserPlotIds(userPlotIds);

        //通过小区id查询单元id
        LambdaQueryWrapper<BuildUnit> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(BuildUnit::getLotId, userPlotIds);
        queryWrapper.eq(BuildUnit::getDelFlag, 0);
        queryWrapper.eq(po.getPlotId()!= null, BuildUnit::getLotId, po.getPlotId());
        queryWrapper.eq(po.getBuildId()!= null, BuildUnit::getBuildId, po.getBuildId());
        queryWrapper.eq(po.getUnitId()!= null, BuildUnit::getId, po.getUnitId());
        List<BuildUnit> buildUnits = buildUnitService.list(queryWrapper);
        if(CollectionUtils.isEmpty(buildUnits)){
            return TableDataInfo.getDataTable(list);
        }
        List<Long> buildUnitIds = buildUnits.stream().map(BuildUnit::getId).collect(Collectors.toList());
        po.setBuildUnitIds(buildUnitIds);
        startPagePost(po);
        return managementService.listBuildUnit(po);
    }

    @ApiOperation("导出单元-详细数据")
    @Log(title = "导出单元-详细数据", businessType = BusinessType.EXPORT)
    @PostMapping("/listBuildUnitExport")
    public void listBuildUnitExport(HttpServletResponse response, @Validated DataManagementPo po) {
//        po.setExportExcel(true);
        po.setPageSize(99999999);
        TableDataInfo<DataManagementVo> list = managementService.listBuildUnit(po);
        ExcelUtil<DataManagementUnitVo> util = new ExcelUtil<DataManagementUnitVo>(DataManagementUnitVo.class);
        util.exportExcel(response, list.getRows().stream().map(item -> {
            DataManagementUnitVo vo = new DataManagementUnitVo();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList()), "单元-详细数据");
    }

    @ApiOperation("住户-详细数据查询")
    @PostMapping("/listHousehold")
    public TableDataInfo<DataManagementVo> listHousehold(@Validated @RequestBody DataManagementPo po) {
        startPagePost(po);
        return managementService.listHousehold(po);
    }


    @ApiOperation("导出住户-详细数据")
    @Log(title = "导出住户-详细数据", businessType = BusinessType.EXPORT)
    @PostMapping("/listHouseholdExport")
    public void listHouseholdExpor(HttpServletResponse response, @Validated DataManagementPo po) {
        po.setPageSize(999999999);
        TableDataInfo<DataManagementVo> list = managementService.listHousehold(po);
        ExcelUtil<DataManagementVo> util = new ExcelUtil<DataManagementVo>(DataManagementVo.class);
        util.exportExcel(response, list.getRows(), "住户-详细数据");
    }

    @ApiOperation("字段控制新增")
    @Log(title = "列表字段控制新增", businessType = BusinessType.EXPORT)
    @PostMapping("/fieldInsert")
    public AjaxResult fieldControlInsert(@RequestBody List<FiledControl> list) {
        return managementService.fieldControl(list);
    }

    @ApiOperation("字段控列表展示")
    @Log(title = "字段控列表展示", businessType = BusinessType.EXPORT)
    @PostMapping("/fieldList")
    public TableDataInfo<FiledControl> fieldControlList(@RequestBody FiledControl list) {
        return managementService.fieldControlList(list);
    }
}
